package demo.pay;

import com.alibaba.fastjson.JSONObject;
import demo.pay.utils.HttpUtil;
import demo.pay.utils.MD5Util;
import demo.pay.utils.Response;

public class Request {
    final String baseUrl = "https://oli.bjphbj.com:8080";
    final String getGoods = baseUrl+"/open/goods";
    final String getAgencyInfo = baseUrl+"/open/info";
    final String postRecharge = baseUrl+"/open/recharge";
    final String postQueryOrder = baseUrl+"/open/query";
    final String sn = "sn12345";
    final String key = "key12345";


    public static void main(String[] args) {
        Request request = new Request();
        request.getAgencyInfo();
        request.getGoods();
        request.postRecharge();
        request.postQueryOrder();
    }

    private void getGoods(){
        try {
            Response response = HttpUtil.doGet(getGoods, sn + " " + MD5Util.MD5Encode(key));
            System.out.println("httpStatus:"+response.getHttpStatus());
            System.out.println("body:"+response.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getAgencyInfo(){
        try {
            Response response = HttpUtil.doGet(getAgencyInfo, sn + " " + MD5Util.MD5Encode(key));
            System.out.println("httpStatus:"+response.getHttpStatus());
            System.out.println("body:"+response.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //返回状态码为 202，成功之后，会通过 notify 指定的 URL 进行通知； 或是通过查询接口进行查询
    private void postRecharge(){
        try {
            JSONObject body = new JSONObject();
            body.put("goodsID",3);
            body.put("cardNO","1234567890987656789");
            body.put("notifyURL","http://127.0.0.1:8080/callback");
            body.put("tradeNO","G123456");

            Response response = HttpUtil.doPost(postRecharge, body.toString() , sn + " " + MD5Util.MD5Encode(body.toString()+key));
            System.out.println("httpStatus:"+response.getHttpStatus());
            System.out.println("body:"+response.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void postQueryOrder(){
        try {
            JSONObject body = new JSONObject();
            body.put("tradeNO","G123456");
            Response response = HttpUtil.doPost(postQueryOrder, body.toJSONString() , sn + " " + MD5Util.MD5Encode(body.toString()+key));
            System.out.println("httpStatus:"+response.getHttpStatus());
            System.out.println("body:"+response.getBody());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
